﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.IO;
using System.Threading;
using System.Collections;

namespace XISoft.AppCreator.Util.Common
{
    public class BatRunner:MessageHelper
    {
        public static Boolean Running = false;

        private readonly String[] szPath=new String[3];
        private Hashtable EnvironmentVariables = new Hashtable();

        public BatRunner( String initFile,String runFile,String stopFile)
        {
            szPath[0] = initFile;
            szPath[1] = runFile;
            szPath[2] = stopFile;

            Running = true;
        }

     

        public void SetEnvironmentVariables(String key,String value)
        {
            PostMessage("[Info] 设置环境变量：" + key + " = "+ value);
            EnvironmentVariables[key] = value;
        }


        public void Run()
        {
            Process process = null;
            int step = 0;
            Boolean post = false;

            try
            {
                LogHelper.WriteStartLogging("start build");
                while (step < 3)
                {
                    process = Run(szPath[step]);
                    //StreamReader outStream = process.StandardOutput;
                    //StreamReader errorStream = process.StandardError;
                    process.WaitForExit(1000);
                    post = false;
                    while (Running)
                    {
                        //if (outStream.Peek() > 0)
                        //{
                            //String msg = outStream.ReadLine();
                            //LogHelper.WriteInfo(typeof(BatRunner).GetType().FullName, msg);
                            //Trace.WriteLine(msg);
                            //continue;
                        //}
                        //else
                        if (!process.HasExited)
                        {
                            if (!post && step == 1)
                            {
                                PostMessage("ready");
                                post = true;
                            }
                        }
                        else
                            break;
                        Thread.Sleep(150);

                    }

                    //outStream.Dispose();
                    //outStream.Close();
                    //errorStream.Dispose();
                    //errorStream.Close();

                    if (!process.HasExited) process.Kill();
                    step++;
                }
            }
            catch (Exception e)
            {
                LogHelper.Trace(e);
            }
            finally
            {
                LogHelper.WriteStopLogging("end build");
            }
        }
        private Process Run(String file)
        {
            Process listFiles = null;
            if (file != null && File.Exists(file))
            {
                try
                {
                    ProcessStartInfo psi = new ProcessStartInfo(file);

                    //psi.RedirectStandardOutput = true;
                    // psi.RedirectStandardError = true;
                    psi.WindowStyle = ProcessWindowStyle.Hidden;
                    psi.UseShellExecute = false;
                    psi.CreateNoWindow = true;

                    foreach (String key in EnvironmentVariables.Keys)
                        psi.EnvironmentVariables.Add(key, EnvironmentVariables[key].ToString());


                    listFiles = Process.Start(psi);

                }
                catch (Exception e)
                {
                    LogHelper.Trace(e);
                    PostMessage("[Error] 运行出错：" + file);
                }
            }
            else
                PostMessage("[Error] 文件：" + file + "不存在");
            return listFiles;
        }
    }
}
